<?php

namespace App\Services;

use App\Models\SmsLog;

use Overtrue\EasySms\EasySms;
use Overtrue\EasySms\Exceptions\Exception;

class SmsService
{

    public static $config = [
        // HTTP 请求的超时时间（秒）
        'timeout' => 5.0,

        // 默认发送配置
        'default' => [
            // 网关调用策略，默认：顺序调用
            'strategy' => \Overtrue\EasySms\Strategies\OrderStrategy::class,

            // 默认可用的发送网关
            'gateways' => [
                // 'aliyun',
                'yuntongxun'
            ],
        ],
        // 可用的网关配置
        'gateways' => [
            'errorlog' => [
                'file' => '/tmp/easy-sms.log',
            ],
            // 			'aliyun' => [
            // 				'access_key_id' => 'LTAI5tGn9FJ4LYAzL3BaftP4',
            // 				'access_key_secret' => 'aS5AJU6ZZeGVq0el3ITjeFUX9U9g8P',
            // 				'sign_name' => '凯斯通',
            // 			],
            'yuntongxun' => [
                'app_id'         => '2c94811c8cd4da0a018f7ae17daf0aca', // 妥投
                'account_sid'    => '8aaf0708591604f301591742cc90010f',
                'account_token'  => 'd925c1d7247144cfb0e16c174f10d618',
                'is_sub_account' => false,
            ]
            //...
        ],
        // 		'SMS' => [
        // 			'IDCARD' => 'SMS_215080274', // 身份验证验证码
        // 			'LOGIN' => 'SMS_215080273', // 登录确认验证码
        // 			'REG' => 'SMS_215080271', // 用户注册验证码
        // 			'PWD' => 'SMS_215080270', // 修改密码验证码
        // 			'INFO' => 'SMS_215080269', // 信息变更验证码
        // 			'CURRENCY' => 'SMS_215480242', //通用

        // 		],
        'SMS' => [
            'REG' => '2587250', // 用户注册验证码
            'FORGOT' => '2587253', // 忘记密码
            'INFO' => '2587256', // 信息变更验证码
            'PWD' => '2587253', // 修改密码验证码

        ],
    ];




    public static function  sendCode($phone, $code, $template)
    {

        $easySms = new EasySms(self::$config);
        try {
            SmsLog::create(
                [
                    'phone' => $phone,
                    'code'    => $code,
                    'add_ip' => request()->ip(),
                    'status'    => 0,
                    'add_time'    => date('Y-m-d H:i:s', time()),
                ]
            );
            $res = $easySms->send($phone, [
                // 'content'  => '您的验证码为: 6379',
                'template' => self::$config['SMS'][$template],
                'data' => [
                    // 	'code' => $code
                    $code
                ],
            ]);

            return $res;
        } catch (Exception $e) {
            noticeLog(json_encode($e));
            // dd($e);
            return [];
        }
    }
}
